/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ambimmort.ucagent.modules.server.dpi;

import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiendpointbean;
import com.ambimmort.ucagent.modules.server.channels.ClientChannel;
import com.ambimmort.ucagent.modules.server.channels.IChannel;
import com.ambimmort.ucagent.modules.server.channels.ServerChannel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author 定巍
 */
public class DPIEndPoint {

    private Dpiendpointbean dpiEndPointBean = null;

    public DPIEndPoint(Dpiendpointbean dpiEndPointBean) {
        this.dpiEndPointBean = dpiEndPointBean;
        clientChannel = new ClientChannel(this);
        serverChannel = new ServerChannel(this);
    }

    private ClientChannel clientChannel;
    private IChannel serverChannel;

    public Dpiendpointbean getDpiEndPointBean() {
        return dpiEndPointBean;
    }

    public void setDpiEndPointBean(Dpiendpointbean dpiEndPointBean) {
        this.dpiEndPointBean = dpiEndPointBean;
    }

    public ClientChannel getClientChannel() {
        return clientChannel;
    }

    public void setClientChannel(ClientChannel clientChannel) {
        this.clientChannel = clientChannel;
    }

    public IChannel getServerChannel() {
        return serverChannel;
    }

    public void setServerChannel(IChannel serverChannel) {
        this.serverChannel = serverChannel;
    }
    
    public synchronized void persistence(){
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = s.beginTransaction();
        Query q = s.createQuery("UPDATE Dpiendpointbean dpi SET dpi.isServerChannelConnected=:issc, dpi.isClientChannelConnected=:iscc WHERE dpi.id=:id");
        q.setParameter("issc", this.dpiEndPointBean.isIsServerChannelConnected());
        q.setParameter("iscc", this.dpiEndPointBean.isIsClientChannelConnected());
        q.setParameter("id", this.dpiEndPointBean.getId());
        q.executeUpdate();
        tx.commit();
        s.close();
        
    }

    @Override
    public String toString() {
        return this.dpiEndPointBean.toString();
    }
    
    
    
}
